Ext.ns('journal');

journal.Utility = function() {

    // private variables

    // private functions

    var logger = journal.LoggerFactory.getLogger("journal.Utility");

    // public space
    return {
        // public properties, e.g. strings to translate

        // public methods

        /**
         * Checks if the array contains the specified object.
         * 
         * @param arr - the array instance to search.
         * @param x - the target item to look for
         * @return true if the parameter x is found in the parameter arr otherwise false.
         */
        arrayContains : function contain(arr, x) {
            for ( var j = 0; j < arr.length; j++) {
                if (arr[j] == x) {
                    return true;
                }
            }
            return false;
        },

        /**
         * Creates a new instance of array from a given array instance with one item less.
         * 
         * @param arr - source array.
         * @param x - the item to remove from the source array.
         * @return a new instance of array equivalent to parameter array minus parameter x.
         */
        arrayRemove : function(arr, x) {
            var newArr = new Array();//from []

            for ( var i = 0; i < arr.length; i++) {
                var y = arr[i];
                if (y != x) {
                    newArr.push(y);
                }
            }
            return newArr;
        },

        doStandardError : function(respObj) {
            Ext.Msg
                    .show({
                        icon : Ext.MessageBox.ERROR,
                        buttons : Ext.Msg.OK,
                        title : 'Journal Error',
                        autoHeight : true,
                        autoWidth : true,
                        msg : respObj.errorDescShort
                                + '<br><br><div id="errorSection"><a id="errorLink">Show Detail</a><div id="errorDetail"/></div>'
                    });

            Ext.EventManager.on(Ext.get('errorLink'), 'click', function() {
                if (Ext.get('errorLink').dom.innerHTML == 'Show Detail') {
                    Ext.get('errorLink').dom.innerHTML = 'Hide Detail';
                    Ext.get('errorDetail').update(respObj.errorDescLong);

                } else {
                    Ext.get('errorLink').dom.innerHTML = 'Show Detail';
                    Ext.get('errorDetail').update('');
                }
            });
        },
        /**
         * Returns a trimmed string. (e.g. limitString('1234567890', 8) == '12345...')
         */
        limitString : function(s, i) {
            if (s.length > i) {
                return s.substring(0, i - 3) + '...';
            } else {
                return s;
            }
        },

        showSessionExpired : function() {
            Ext.MessageBox.show({
                title : 'Session Expired',
                msg : 'You will now be redirected to login page.',
                buttons : Ext.MessageBox.OK,
                icon : Ext.MessageBox.WARNING,
                fn : function() {
                    window.location = './login';
                }
            });
        }

    };
}(); // end of app

// end of file

